package com.lxy.cxcyxmmsbackend.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxy.cxcyxmmsbackend.sys.entity.KeyValue;
import com.lxy.cxcyxmmsbackend.sys.res.Result;
import com.lxy.cxcyxmmsbackend.sys.service.KeyValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Key;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 刘新颖
 * @since 2024-02-20
 */
@Api(tags = "字典表")
@RestController
@RequestMapping("/keyValue")
public class KeyValueController {

    @Autowired
    KeyValueService keyValueService;

    @ApiOperation("查询字典")
    @RequestMapping("/queryKey")
    public Result queryKey(@RequestParam("keyCodes") String keyCodes) {
        HashMap<String, Object> resultMap = new HashMap<>();
        List<String> keyCodeList = Arrays.asList(keyCodes.split(","));
        for (String keyCode : keyCodeList) {
            QueryWrapper<KeyValue> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(KeyValue::getKeycode, keyCode);
            List<KeyValue> list = keyValueService.list(queryWrapper);
            KeyValue keyValue = new KeyValue();
            keyValue.setKeyname("全部");
            keyValue.setKeycode(keyCodes);
            keyValue.setKeyvalue("");
            list.add(keyValue);
            if (list != null && list.size()>0){
                resultMap.put(keyCode,list);
            }
        }
        return Result.success("查询字典成功",resultMap);
    }

}

